﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Common;
using MarginTrading.Services;

namespace MarginTrading.Forms
{
    public partial class AllUsers : Form
    {
        public AllUsers()
        {
            InitializeComponent();
        }

        private void AllUsers_Load(object sender, EventArgs e)
        {
            try
            {
                DataTable userTable = UserAccountServices.GetCustomUserAccountCollectionDataTable();
	            dataGridViewUsers.DataSource = userTable;
	
	            dataGridViewUsers.Width = Size.Width - 30;
	            dataGridViewUsers.Height = Size.Height - 140;
	
	            dataGridViewUsers.Columns[0].Width = (dataGridViewUsers.Width * 30) / 100;
                dataGridViewUsers.Columns[1].Width = ((dataGridViewUsers.Width * 70) / 100) - 2;
            }
            catch (System.Exception ex)
            {
            	
            }
  
            //System.Drawing.Size size = new System.Drawing.Size();
            //size.Height = dataGridViewUsers.Height + 100;
            //size.Width = 20;
            //this.MinimumSize = size;

            WindowState = FormWindowState.Maximized;
        }

        private void AllUsers_SizeChanged(object sender, EventArgs e)
        {
            dataGridViewUsers.Height = Size.Height - 140;
            dataGridViewUsers.Width = Size.Width - 30;

            dataGridViewUsers.Columns[0].Width = (dataGridViewUsers.Width * 30) / 100;
            dataGridViewUsers.Columns[1].Width = ((dataGridViewUsers.Width * 70) / 100) - 2;
        }

        private void dataGridViewUsers_CurrentCellChanged(object sender, EventArgs e)
        {
        }

        private void dataGridViewUsers_MouseUp(object sender, MouseEventArgs e)
        {
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {

        }

        //Add
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            AddUser oAddUserForm = new AddUser();
            oAddUserForm.ShowDialog();

            try
            {
	            DataTable userTable = UserAccountServices.GetUserAccountCollectionDataTable();
	            dataGridViewUsers.DataSource = userTable;
	            dataGridViewUsers.Refresh();
            }
            catch (System.Exception ex)
            {
            	
            }
        }

        //delete
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (dataGridViewUsers.SelectedRows.Count == 0)
            {
                MessageBox.Show("No record selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string userName = dataGridViewUsers.SelectedRows[0].Cells[0].Value.ToString();

            UserAccount user = null;
            try
            {
                user = UserAccountServices.GetUserAccountByUserName(userName);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Invalid Username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (user == null)
            {
                MessageBox.Show("Invalid Username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (user.UserName == Program.currentUser.UserName)
            {
                MessageBox.Show("You cannot delete your own user account", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            DialogResult result = MessageBox.Show("Do you really want to delete user account " + userName + "?", "Confirmation", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

            if (result != DialogResult.Yes)
            {
                return;
            }
            
            try
            {
                UserAccountServices.Delete(userName);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Failed to delete user account\nError: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            dataGridViewUsers.Rows.RemoveAt(dataGridViewUsers.SelectedRows[0].Index);
            MessageBox.Show("User account deleted successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void toolStripButtonEdit_Click(object sender, EventArgs e)
        {
            if (dataGridViewUsers.SelectedRows.Count == 0)
            {
                MessageBox.Show("No record selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            EditUser editUserDlg = new EditUser(dataGridViewUsers.SelectedRows[0].Cells[0].Value.ToString());
            editUserDlg.ShowDialog();

            try
            {
	            DataTable userTable = UserAccountServices.GetUserAccountCollectionDataTable();
	            dataGridViewUsers.DataSource = userTable;
	            dataGridViewUsers.Refresh();
            }
            catch (System.Exception ex)
            {
            	
            }
        }

        private void dataGridViewUsers_SelectionChanged(object sender, EventArgs e)
        {
        }

        private void dataGridViewUsers_CellClick(object sender, DataGridViewCellEventArgs e)
        {
        }
    }
}
